<?php
/*
	Tiny Cache - A very simple cache system
	Author: iStef (Liu Yang)
	Author Email: istef.liu@gmail.com
	HomePage: http://blog.istef.info
	This class is published under GNU GPL License. You can use/disturb with your work freely.
*/
if (!defined("CACHE_FILE_DIR")) {
	define("CACHE_FILE_DIR", dirname(__FILE__)."/cache");
}
if (!defined("CACHE_FORCE_NOGZIP")) {
	define("CACHE_FORCE_NOGZIP", 0);			// Set it to non-zero value will not use gzip
}
class TinyCache {
	var $expired		=	3600;				// When files are stale, 1 hour default
	
	var $_cache_dir		=	CACHE_FILE_DIR;
	var $_use_gzip		=	true;
	function cCache($expired = false) {
		if ($expired) $this->expired = $expired;
		if (function_exists("gzcompress") && (CACHE_FORCE_NOGZIP == 0)) {
			$this->_use_gzip = true;
		} else {
			$this->_use_gzip = false;
		}
		$this->_cache_dir = CACHE_FILE_DIR;
		if (!file_exists(CACHE_FILE_DIR) || (fileperms(CACHE_FILE_DIR) & 0x4000) != 0x4000) {
			$s = @mkdir($this->_cache_dir, 0755);
			if ($s) {
				return true;
			} else {
				return false;
			}
		} elseif (!is_writable($this->_cache_dir)) {
			$s = @chmod($this->_cache_dir, 0755);
			if ($s) {
				return true;
			} else {
				return false;
			}
		} else {
			return true;
		}
	}
	
	function set($sig,$content) {
		$fp = $this->_fopen($this->_file_name($sig) , "w");
		if (!$fp) {
			return false;		// could not create/write cache file.
		} else {
			$this->_fwrite($fp, $this->_encode($content));
			$this->_fclose($fp);
			return true;
		}
	}
	function get($sig) {
		$filename = $this->_file_name($sig);
		if (file_exists($filename)) {
			$fp = $this->_fopen($filename, "r");
			if (!$fp) return false;
			$rtn_data = $this->_fread($fp);
			$rtn_data = $this->_decode($rtn_data);
			$this->_fclose($fp);
			return $rtn_data;
		} else {
			return false;
		}
	}
	function check($sig) {
		$now = time();
		$filename = $this->_file_name($sig);
		if (file_exists($filename)) {
			$ftime = filemtime($filename);
			$duration = $now - $ftime;
			if ($duration < $this->expired) {
				return "HIT";
			} else {
				return "EXPIRED";
			}
		} else {
			return "MISS";
		}
	}
	function touch($sig) {
		$filename = $this->_file_name($sig);
		if (file_exists($filename)) {
			$s = touch($filename);
			if ($s) return true;
			else return false;
		} else {
			return false;
		}
	}
	function mtime($sig) {
		$filename = $this->_file_name($sig);
		if (file_exists($filename)) {
			return filemtime($filename);
		} else {
			return false;
		}
	}
	function _encode($content) {
		return serialize($content);
	}
	function _decode($content) {
		return unserialize($content);
	}
	function _file_name($sig) {
		$rtn = $this->_cache_dir . "/";
		$rtn .= 'alimama_deluxe_'.md5($sig);
		if ($this->_use_gzip) $rtn .= ".gz";
		//var_dump($rtn);
		return $rtn;
	}
	function _fopen($fname, $fope) {
		if ($this->_use_gzip) {
			return @gzopen($fname, $fope);
		} else {
			return @fopen($fname, $fope);
		}
	}
	function _fclose($fp) {
		if ($this->_use_gzip) {
			return gzclose($fp);
		} else {
			return fclose($fp);
		}
	}
	function _fread($fp) {
		$rtn = "";
		if ($this->_use_gzip) {
			while (!gzeof($fp)) {
				$rtn .= gzread($fp, 4096);
			}
		} else {
			while (!feof($fp)) {
				$rtn .= fread($fp, 4096);
			}
		}
		return $rtn;
	}
	function _fwrite($fp, $fcontent) {
		if ($this->_use_gzip) {
			return @gzwrite($fp, $fcontent);
		} else {
			return @fwrite($fp, $fcontent);
		}
	}
}
?>
